<template>
	<view>
		<u-toast ref="uToast" />
		<u-no-network></u-no-network>
		<u-navbar title="提交订单" safeAreaInsetTop fixed placeholder>
			<view class="coreshop-navbar-left-slot" slot="left">
				<u-icon name="arrow-left" size="19" @click="goNavigateBack"></u-icon>
				<u-line direction="column" :hairline="false" length="16" margin="0 8px"></u-line>
				<u-icon name="home" size="22" @click="goHome"></u-icon>
			</view>
			<view slot="right">
			</view>
		</u-navbar>
		<view class="content">
			<view class="coreshop-margin-left-10 coreshop-margin-right-10 coreshop-margin-top-10">
				<u-alert :title="title" type="warning" effect="dark" :description="description"></u-alert>
			</view>
			<view class="coreshop-margin-left-10 coreshop-margin-right-10 coreshop-margin-top-10">
				<u-subsection 
					:list="typeItems" 
					mode="subsection" 
					:current="typeCurrent" 
					@change="onTypeItem" 
					bg-color="#ffffff"
					active-color="#e54d42" 
					v-if="typeItems.length > 0" 
					style="border-radius:0;"
				>
				</u-subsection>
			</view>
			<view class="content">
				<view
					v-show="OrderDistributionModelCurrent === 'isOpenHomeDelivery' || OrderDistributionModelCurrent === 'isOpenMailing'">
					<!-- 收货地址信息 -->
					<view class="coreshop-margin-top-10 coreshop-margin-bottom-10 coreshop-common-view-box"
						v-if="userShip && userShip.id" @click="showAddressList">
						<view class="coreshop-bg-white coreshop-card address-view">
							<view class="coreshop-list menu-avatar">
								<view class="coreshop-list-item">
									<view class="icon-view">
										<u-icon name="map" size="18"></u-icon>
									</view>
									<view class="content">
										<view class="coreshop-text-black">
											<text>收货人：{{ userShip.name || '' }}</text>
											<text class="coreshop-margin-left-10">{{ userShip.mobile || ''}}</text>
										</view>
										<view class="coreshop-text-gray coreshop-font-sm flex">
											<view class="u-line-2">{{ userShip.areaName || ''}}
												{{userShip.street || ''}} {{userShip.address || ''}}
											</view>
										</view>
									</view>
									<view class="action coreshop-text-gray">
										<u-icon name="arrow-right" @click="goAddress()"></u-icon>
									</view>
								</view>
							</view>
							<view class="address-line" />
						</view>
					</view>
					<view class="coreshop-margin-top-10 coreshop-margin-bottom-10 coreshop-common-view-box" v-else>
						<view
							class="coreshop-bg-white coreshop-card address-view coreshop-text-center coreshop-padding-bottom-30 coreshop-flex coreshop-justify-center">
							<view class="coreshop-width-fit-content">
								<u-button type="error" size="mini" @click="goAddress()">添加收货地址</u-button>
							</view>
							<view class="address-line" />
						</view>
					</view>
				</view>
				<view v-show="OrderDistributionModelCurrent === 'isOpenSelfDelivery'">
					<!-- 门店信息 -->
					<view class="coreshop-margin-top-10 coreshop-margin-bottom-10 coreshop-common-view-box"
						v-if="store && store.id">
						<view class="coreshop-bg-white coreshop-card address-view">
							<view class="coreshop-list menu-avatar">
								<view class="coreshop-list-item">
									<view class="icon-view">
										<u-icon name="map" size="18"></u-icon>
									</view>
									<view class="content">
										<view class="coreshop-text-black">
											<text>{{store.name|| ''}}</text>
											<text class="coreshop-margin-left-10">{{store.mobile|| ''}}</text>
										</view>
										<view class="coreshop-text-gray coreshop-font-sm flex">
											<view class="u-line-2">{{store.address|| ''}}</view>
										</view>
									</view>
									<view class="action coreshop-text-gray">
										<!-- <u-icon name="arrow-right" @click="goStorelist()"></u-icon> -->
									</view>
								</view>
							</view>
							<view class="address-line" />
						</view>
					</view>
					<!-- @click="goStorelist() -->
					<view v-else class='coreshop-margin-top-10 coreshop-margin-bottom-10 coreshop-common-view-box'>
						<view class="coreshop-bg-white coreshop-card address-view">
							<view class='coreshop-padding-10 coreshop-text-center'>暂无门店</view>
							<view class="address-line" />
						</view>
					</view>
				</view>
			</view>
			<view class='coreshop-margin-top-10 coreshop-margin-bottom-10 coreshop-common-view-box'
				v-if="storeSwitch == 1 && OrderDistributionModelCurrent === 'isOpenSelfDelivery'">
				<view class="coreshop-form-group">
					<view class="title">姓名</view>
					<input class='coreshop-cell-bd-input' placeholder='请输入提货人姓名' v-model="storePick.name"
						style="width: 100%;"></input>
				</view>
				<view class="coreshop-form-group">
					<view class="title">电话</view>
					<input class='coreshop-cell-bd-input' placeholder='请输入提货人电话' v-model="storePick.mobile"
						style="width: 100%;"></input>
				</view>
			</view>
			<!-- 商品列表信息 -->
			<!--商品信息-->
			<view class="coreshop-margin-top-10 coreshop-margin-bottom-10 coreshop-common-view-box">
				<view class="coreshop-bg-white coreshop-card goods-view">
					<view class="goods-info-view-box coreshop-solid-bottom" v-if="item.isSelect == true" v-for="(item, index) in products" :key="index">
						<view 
							class="coreshop-avatar radius lg" 
							:style="[{backgroundImage:'url('+ item.products.images+'?x-oss-process=image/resize,m_lfit,h_200,w_200' +')'}]"
						/>
						<view class="goods-info-view">
							<view class="coreshop-text-black u-line-2">{{ item.products.name || ''}}</view>
							<view class="coreshop-font-sm coreshop-text-gray" v-if="item.products.spesDesc !== null">
								{{ item.products.spesDesc || ''}}
							</view>
							<view class="coreshop-tag-view" v-if="item.products.promotionList">
								<u-tag :text="v.name" mode="light" size="mini" v-for="(v, k) in item.products.promotionList" :key="k" />
							</view>
							<view class="goods-price-view">
								<view class="coreshop-flex coreshop-flex-direction-row">
									<text
										class="coreshop-text-price coreshop-text-red coreshop-font-lg">{{ item.products.price || ''}}</text>
									<view class="coreshop-flex coreshop-flex-direction-row coreshop-font-10 coreshop-align-center"
										v-if="pointSwitch==1 && pointExchangeModel==2 && pointShowExchangePrice==1 && item.products.pointsDeduction > 0  && orderType== $globalConstVars.paymentType.common">
										<view>
											（{{ pointShowName}}兑换价:
										</view>
										<view class="coreshop-text-red">
											{{pointDiscountedProportion * item.products.pointsDeduction }}{{ pointShowName}}+{{ parseFloat(item.products.price - item.products.pointsDeduction).toFixed(2)}}元
										</view>
										）
									</view>
								</view>
								<view class='coreshop-text-right goods-num'>× {{ item.nums || ''}}</view>
							</view>
						</view>

						<view class="coreshop-flex coreshop-flex-direction-row coreshop-font-13 coreshop-margin-top-10"
							v-if="pointSwitch==1 && pointExchangeModel==2 && pointShowExchangePrice==1 && item.products.pointsDeduction > 0  && orderType== $globalConstVars.paymentType.common">
							<view>
								{{ pointShowName}}兑换价合计：
							</view>
							<view class="coreshop-text-red">
								{{pointDiscountedProportion * item.products.pointsDeduction * item.nums }}{{ pointShowName}}+{{parseFloat((item.products.price - item.products.pointsDeduction)*item.nums).toFixed(2) }}元
							</view>
						</view>
						<view class="coreshop-flex coreshop-flex-direction-row coreshop-font-13 coreshop-margin-top-10"
							v-if="pointSwitch==1 && pointGetModel==2 && pointShowPoint==1 && item.products.points > 0">
							<view>
								购买将赠送：
							</view>
							<view class="coreshop-text-red">
								{{item.products.points * item.nums }}{{ pointShowName}}
							</view>
						</view>

					</view>
				</view>
			</view>

			<!--优惠券-->
			<view class="coreshop-margin-top-10 coreshop-margin-bottom-10 coreshop-common-view-box"
				v-if="userCoupons.length>0 && orderType== $globalConstVars.paymentType.common && isShowCoupon">
				<view class="coreshop-bg-white coreshop-card goods-view">
					<view class="coreshop-text-black coreshop-gift-list">
						<view class="u-line-1 title">优惠券</view>
						<view @click="notUseCoupon()" v-if="usedCoupons.length>0" class="closeTag">
							<u-tag class="coreshop-text-right" text="取消选择" closeable type="error" shape="circle" mode="dark"
								size="mini" />
						</view>
					</view>
					<!--滑动列表-->
					<view class="coupon-scroll-box">
						<scroll-view class="coupon-scroll" scroll-x>
							<block v-for="(item, index) in userCoupons" :key="index">
								<view :id="['scroll' + (index + 1 )]"
									class="coupon-scroll-item coreshop-margin-top-10 flex coreshop-service-view couponBox"
									@click="couponHandle(index)">
									<view class="flex-sub bg-page coreshop-padding-10 coreshop-margin-10 radius ">
										<view class="coreshop-text-black u-line-1">{{ item.couponName || ''}}</view>
										<view class="coreshop-text-gray coreshop-font-sm u-line-1">
											{{ item.expression2 }}
										</view>
										<view class="coreshop-text-black">
											<text class="line-red coreshop-font-10 radius">有效期：{{ item.stime + ' 至 ' + item.etime }}</text>
										</view>
										<view v-if="!item.checked && item.disabled">
											<view class="coreshop-corner-mark" />
											<view class="check-icon">
												<u-icon name="checkmark" color="#fff"></u-icon>
											</view>
										</view>
										<view v-else-if="item.checked && item.disabled">
											<view class="coreshop-corner-mark check" />
											<view class="check-icon check">
												<u-icon name="checkmark" color="#fff"></u-icon>
											</view>
										</view>
									</view>
								</view>
							</block>
						</scroll-view>
					</view>
				</view>
			</view>
			<!--支付方式-->
			<view class="coreshop-margin-top-10 coreshop-margin-bottom-10 coreshop-common-view-box">
				<view class="coreshop-bg-white coreshop-card pay-view">
					<view class="coreshop-list menu">
						<!-- 商户开启积分 并且用户有积分情况下 -->
						<view class="coreshop-list-item"
							v-if="isOpenPoint === 1 && userPointNums > 0 && orderType== $globalConstVars.paymentType.common">
							<!--<view class="coreshop-list-item arrow">-->
							<view class="content">
								<text class="coreshop-text-black">{{ pointShowName}}抵扣</text>
								<view class="coreshop-text-gray coreshop-font-xs flex">
									<text class="u-line-1">
										可用{{ canUsePoint}}{{ pointShowName}}，可抵扣{{ pointMoney}}元，共有{{ userPointNums}}{{ pointShowName}}
									</text>
								</view>
							</view>
							<view class="action" @click="changePointHandle">
								<view class="coreshop-text-gray fr">
									<switch :class="isUsePoint?'checked':''" :checked="isUsePoint"></switch>
								</view>
							</view>
						</view>
						<view class="coreshop-list-item" v-if="invoiceSwitch == 1" @click="goInvoice()">
							<view class="content">
								<text class="coreshop-text-black">发票</text>
							</view>
							<view class="coreshop-flex coreshop-flex-direction-row coreshop-justify-between">
								<text class="coreshop-text-gray">{{invoice.name|| '无'}}</text>
								<u-icon name="arrow-right"></u-icon>
							</view>
						</view>
					</view>
				</view>
			</view>
			<!--商品价格计算-->
			<view class="coreshop-margin-top-10 coreshop-margin-bottom-10 coreshop-common-view-box">
				<view class="coreshop-bg-white coreshop-card coreshop-price-view">
					<view class="coreshop-text-black item-view">
						<view class="u-line-1 title">商品总额</view>
						<text class="coreshop-text-red coreshop-text-price coreshop-text-right">{{ cartData.goodsAmount }}</text>
					</view>
					<view class="coreshop-text-black item-view">
						<view class="u-line-1 title">商品优惠</view>
						<text class="coreshop-text-right">- {{ cartData.goodsPromotionMoney || '0'}}</text>
					</view>
					<view class="coreshop-text-black item-view">
						<view class="u-line-1 title">订单优惠</view>
						<text class="coreshop-text-right">- {{ cartData.orderPromotionMoney || '0' }}</text>
					</view>
					<view class="coreshop-text-black item-view">
						<view class="u-line-1 title">优惠券抵扣</view>
						<text class="coreshop-text-right">- {{ cartData.couponPromotionMoney  || '0'}}</text>
					</view>
					<view class="coreshop-text-black item-view"
						v-if="isOpenPoint === 1 && userPointNums > 0 && orderType== $globalConstVars.paymentType.common">
						<view class="u-line-1 title">{{ pointShowName}}抵扣</view>
						<text class="coreshop-text-right">- {{ cartData.pointExchangeMoney || '0'}}</text>
					</view>
					<view class="coreshop-text-black item-view">
						<view class="u-line-1 title">
							<text class="coreshop-margin-right-10">运费</text>
							<text class="cuIcon-question icon" />
						</view>
						<text class="coreshop-text-right">{{ cartData.costFreight || '0'}}</text>
					</view>
				</view>
			</view>
			<!--买家留言-->
			<view class="coreshop-margin-top-10 coreshop-margin-bottom-10 coreshop-common-view-box">
				<view class="coreshop-bg-white coreshop-card coreshop-price-view">
					<view class="title">买家留言</view>
					<view class="coreshop-padding-10">
						<textarea class="memoBox" maxlength="50" @input="memoChange" placeholder="50字以内(选填)"></textarea>
					</view>
				</view>
			</view>
			<!--占位底部距离-->
			<view class="coreshop-tabbar-height" />
			<!--底部操作-->
			<view class="coreshop-bg-white coreshop-footer-fixed coreshop-foot-padding-bottom">
				<view
					class="coreshop-flex coreshop-flex-nowrap coreshop-justify-between  coreshop-padding-10 coreshop-percent-100">
					<view
						class="coreshop-text-black coreshop-text-bold price-view coreshop-basis-6 coreshop-flex coreshop-align-center">
						<text class="coreshop-margin-right-10">共 {{ productNums}} 件商品</text>
						<text>合计<text class="coreshop-text-price coreshop-text-red coreshop-font-lg coreshop-margin-left-10">
								{{ cartData.amount}}</text></text>
					</view>
					<view class="coreshop-basis-4">
						<u-button size="normal" type="error" @click="toPay" :disabled='submitStatus'
							:loading='submitStatus'>确认下单</u-button>
					</view>
				</view>
			</view>
		</view>
		<!-- 登录提示 -->
		<coreshop-login-modal></coreshop-login-modal>
	</view>
</template>
<script>
	import {
		mapState
	} from 'vuex'
	export default {

		data() {
			return {
				typeItems: [],
				//typeItems: ['快递物流', '同城配送', '门店自提'],
				typeCurrent: 0,
				cartData: {}, // 购物车商品详情
				products: [], // 货品信息
				promotions: [], // 促销信息
				userShip: {}, // 用户收货地址
				receiptType: 1, // 订单类型 1快递物流发货订单，2同城配送，3是门店自提订单
				params: {
					ids: 0, // 传递过来的购物车id
					userShipId: 0,
					areaId: 0, // 收货地址id
					couponCode: '', // 优惠券码列表(string)多张逗号分隔
					point: 0, // 抵扣积分额
					type: 1, //购物车类型
					objectId: 0, //关联对象类型
				}, // 监听params参数信息 以重新请求接口
				// 发票信息
				invoice: {
					type: 1, // 类型 1不开发票 2个人发票 3公司发票
					name: '', // 发票抬头
					code: '' // 发票税号
				},
				memo: '', // 买家留言
				items: [
					'选择优惠券',
					'输入券码',
				],
				orderType: 1, // 商品订单类型 1
				current: 0,
				isUsePoint: false, // 是否勾选使用积分
				userPointNums: 0, // 用户的总积分
				canUsePoint: 0, // 可以使用的积分
				pointMoney: 0, // 积分抵扣的金额
				userCoupons: [], // 用户的可用优惠券列表
				usedCoupons: {}, // 已经选择使用的优惠券
				inputCouponCode: '', // 输入的优惠券码
				optCoupon: '', // 当前选择使用的优惠券(暂存使用 如果接口返回不可用则剔除优惠券状态)
				store: {
					id: 0,
					name: '',
					mobile: '',
					address: ''
				},
				storePick: {
					name: '',
					mobile: ''
				},
				objectId: 0, //关联对象序列
				teamId: 0, //拼团订单分组序列
				submitStatus: false,
				isOpenHomeDelivery: true,
				isOpenMailing: true,
				isOpenSelfDelivery: true,
				OrderDistributionModels: [],
				OrderDistributionModelCurrent: '',
				requireOrder: 0, //自定义交易组件是否需要推单，1:需要，0:不需要
				requiredFundType: 0, //requireOrder = 1时生效，0，非二级商户号订单，1，二级商户号订单，2，两种方式皆可（后续只会存在1）
				traceId: '', //跟踪ID，有效期十分钟，会影响主播归因、分享员归因等，需创建订单前调用，调用生成订单 api 时需传入该参数
				title: '下单提醒',
				description: '选择【同城配送】下单将产生运费，由我们进行配送，选择【门店自提】下单，请前往门店提供订单手机号码或者核销码进行核销取货。'
			}
		},
		components: {},
		onLoad(options) {
			var _this = this;
			this.storePick = {
				name: this.userInfo.nickName,
				mobile: this.userInfo.mobile,
			}
			//下单前置检查，自定义交易组件检测场景是否需要推送订单到微信服务。
			// #ifdef APP-PLUS|APP-PLUS-NVUE
			_this.getOrderDistributionModel()
			// #endif
			// #ifdef MP
			uni.getSystemInfo({
				success: (res) => {
					//console.log("所获取设备数据皆来自设备本身", res)
					var systemInfo = res;
					if (systemInfo.deviceType != 'pc') {
						var values = systemInfo.version.split('.');
						var v1 = parseInt(values[0]);
						var v2 = parseInt(values[1]);
						var v3 = parseInt(values[2]);
						if (v1 >= 8 && v2 >= 0 && v3 >= 18) {
							wx.checkBeforeAddOrder({
								success(res) {
									if (res.errMsg == 'checkBeforeAddOrder:ok') {
										_this.requireOrder = res.data.requireOrder;
										_this.requiredFundType = res.data.requiredFundType;
										_this.traceId = res.data.traceId;
										// 获取不同类型营销下单支持的配送方式
										_this.getOrderDistributionModel()
									}
								},
								fail(res) {
									// 获取不同类型营销下单支持的配送方式
									_this.getOrderDistributionModel()
								}
							})
						} else {
							_this.getOrderDistributionModel();
						}

					} else {
						_this.getOrderDistributionModel();
					}
				},
				fail(res) {
					_this.getOrderDistributionModel()
				}
			})
			// #endif

			let cartIds = options.cartIds;
			if (options.orderType) {
				this.orderType = options.orderType;
				this.params.type = options.orderType;
			}
			if (options.objectId) {
				this.objectId = options.objectId;
				this.params.objectId = options.objectId;
			}
			if (options.teamId) {
				this.teamId = options.teamId;
			}
			this.params.ids = JSON.parse(cartIds)
			if (!this.params.ids) {
				_this.$refs.uToast.show({
					message: '获取失败',
					type: 'error',
					complete: function() {
						uni.navigateBack({
							delta: 1
						});
					}
				})
			}

			// 获取用户的默认收货地址信息
			this.userDefaultShip()
			// 获取用户的可用优惠券信息
			this.getUserCounpons()
			//获取默认门店信息
			this.getDefaultStore();
		},
		onShow() {
			// #ifdef MP-ALIPAY || MP-TOUTIAO
			let userShip = this.$db.get('addressUserShip', true);
			if (userShip) {
				this.userShip = userShip;
				this.params.userShipId = userShip.id;
				this.params.areaId = userShip.areaId;
				this.$db.del('addressUserShip', true);
			}
			let userInvoice = this.$db.get('userInvoice', true);
			if (userInvoice) {
				this.invoice = userInvoice;
				this.$db.del('userInvoice', true);
			}
			let userStore = this.$db.get('userStore', true);
			if (userStore) {
				this.store = userStore;
				this.$db.del('userStore', true);
			}
			// #endif
			// #ifdef APP-PLUS || APP-PLUS-NVUE
			let userShip = this.$store.state.userShip;
			if (userShip) {
				this.userShip = userShip;
				this.params.userShipId = userShip.id;
				this.params.areaId = userShip.areaId;
			}
			let userInvoice = this.$store.state.invoice;
			if (userInvoice) {
				this.invoice = userInvoice;
			}
			// #endif
		},
		methods: {
			// 切换门店
			onTypeItem(index) {
				if (this.typeCurrent !== index) {
					this.typeCurrent = index;
				}
				let receiptType = 1;

				this.OrderDistributionModelCurrent = this.OrderDistributionModels[this.typeCurrent];
				if (this.OrderDistributionModelCurrent == 'isOpenMailing') {
					receiptType = 1;
				} else if (this.OrderDistributionModelCurrent == 'isOpenHomeDelivery') {
					receiptType = 2;
				} else if (this.OrderDistributionModelCurrent == 'isOpenSelfDelivery') {
					receiptType = 3;
				}
				this.receiptType = receiptType;
				this.getCartList();
			},
			// 跳转到门店列表
			goStorelist() {
				//console.log("跳转到门店列表");
				this.$u.route('/pages/placeOrder/storeList/storeList')
			},
			// 没有收货地址时跳转
			goAddress() {
				//console.log("没有收货地址时跳转");
				this.$u.route('/pages/member/address/list/list?type=order')
			},
			// 获取不同类型营销下单支持的配送方式
			getOrderDistributionModel() {
				if (this.requireOrder == 1) {
					let newTypeItems = [];
					newTypeItems.push('快递物流');
					this.OrderDistributionModels.push('isOpenMailing');
					this.typeItems = newTypeItems;
					this.OrderDistributionModelCurrent = this.OrderDistributionModels[0];
				} else {
					let data = {
						id: this.orderType
					};
					this.$u.api.getOrderDistributionModel(data).then(res => {
						if (res.status) {
							//物流快递
							this.isOpenMailing = res.data.isOpenMailing;
							//同城配送
							this.isOpenHomeDelivery = res.data.isOpenHomeDelivery;
							//上门自提
							this.isOpenSelfDelivery = res.data.isOpenSelfDelivery;

							if (this.requireOrder == 0) {
								let newTypeItems = [];
								if (this.isOpenMailing) {
									newTypeItems.push('快递物流');
									this.OrderDistributionModels.push('isOpenMailing');
								}
								if (this.isOpenHomeDelivery) {
									newTypeItems.push('同城配送');
									this.OrderDistributionModels.push('isOpenHomeDelivery');
								}
								if (this.isOpenSelfDelivery) {
									newTypeItems.push('门店自提');
									this.OrderDistributionModels.push('isOpenSelfDelivery');
								}
								this.typeItems = newTypeItems;
							}
							this.OrderDistributionModelCurrent = this.OrderDistributionModels[0];
							if (this.OrderDistributionModelCurrent == 'isOpenMailing') {
								this.receiptType = 1;
							} else if (this.OrderDistributionModelCurrent == 'isOpenHomeDelivery') {
								this.receiptType = 2;
							} else if (this.OrderDistributionModelCurrent == 'isOpenSelfDelivery') {
								this.receiptType = 3;
							}
						}
					})
				}
			},
			// 获取用户的默认收货地址
			userDefaultShip() {
				this.$u.api.userDefaultShip().then(res => {
					if (res.status && res.data && Object.keys(res.data).length) {
						this.userShip = res.data
						this.params.userShipId = this.userShip.id;
						this.params.areaId = this.userShip.areaId

						this.storePick.name = res.data.name;
						this.storePick.mobile = res.data.mobile;

					}
				})
			},
			// 获取购物车商品详情
			getCartList() {
				uni.showLoading({
					title: '加载中'
				});
				let _that = this;
				let data = this.params
				data['receiptType'] = this.receiptType // 区分订单类型  1快递物流,2同城配送，3上门自提订单
				this.$u.api.cartList(data).then(res => {
					if (res.status) {
						let data = res.data
						// 判断是否开启积分抵扣 并且 没有勾选积分使用
						if (this.isOpenPoint === 1 && !this.isUsePoint) {
							let money = {
								cartType: this.orderType,
								ids: this.params.ids,
								orderMoney: data.amount
							}
							this.$u.api.usablePoint(money).then(res => {
								if (res.status) {
									this.userPointNums = res.data.point // 用户总积分
									this.canUsePoint = res.data.availablePoint // 可以使用的积分
									this.pointMoney = res.data.pointExchangeMoney // 积分抵扣的总金额
								}
							})
						}
						// 所有价格转换
						//data.amount = this.$common.formatMoney(data.amount);
						//data.goodsAmount = this.$common.formatMoney(data.goodsAmount);
						data.goodsPromotionMoneyOld = data.goodsPromotionMoney;
						//data.goodsPromotionMoney = this.$common.formatMoney(data.goodsPromotionMoney);
						//data.couponPromotionMoney = this.$common.formatMoney(data.couponPromotionMoney);
						data.orderPromotionMoneyOld = data.orderPromotionMoney;
						//data.orderPromotionMoney = this.$common.formatMoney(data.orderPromotionMoney);
						//data.pointExchangeMoney = this.$common.formatMoney(data.pointExchangeMoney);
						//data.costFreight = this.$common.formatMoney(data.costFreight);
						// 购物车详情
						this.cartData = data
						// 商品详情
						this.products = data.list
						//判断是否有库存
						let noStock = true;
						for (let i = 0; i < data.list.length; i++) {
							if (data.list[i].isSelect) {
								noStock = false;
							}
						}
						if (noStock) {
							this.$u.toast("您所挑选的商品已售罄，请重新添加哦");
						}
						// 优惠信息
						this.promotions = data.promotionList
						// 使用的优惠券信息
						this.usedCoupons = data.coupon

						this.inputCouponCode = ''
						this.optCoupon = ''
						uni.hideLoading();
					} else {

						this.$refs.uToast.show({
							message: res.msg,
							type: 'error',
							back: false,
							complete: function() {
								// 优惠券不可用状态判断
								// 优惠券号码不存在 			15009
								// 优惠券未开始				15010
								// 优惠券已使用				15013
								// 优惠券不符合使用规则		15014
								// 优惠券不可使用多张			15015
								// 优惠券已经过期			15011
								let errStatus = [15009, 15010, 15011, 15013, 15014, 15015, ]
								if (errStatus.indexOf(res.data) !== -1) {
									//console.log('删除使用的优惠券号码');
									// 删除使用的优惠券号码
									if (_that.current === 1) {
										_that.removeCouponCode(_that.inputCouponCode, _that.current)
									} else {
										// 取消选择使用的状态
										//console.log(_that.optCoupon);
										if (_that.optCoupon) {
											_that.userCoupons.forEach(item => {
												if (item.couponCode === _that.optCoupon) {
													item.checked = false
												}
											})
										}
										_that.removeCouponCode(_that.optCoupon, _that.current)
										//console.log(_that.optCoupon);
									}
								} else {
									//console.log('未判断出内容');
								}
							}
						})
						uni.hideLoading();
					}
				})
			},
			// 获取用户可用的优惠券信息
			getUserCounpons() {
				let data = {
					display: 'no_used',
					ids: this.params.ids
				}
				this.$u.api.getCartCoupon(data).then(res => {
					if (res.status) {
						let _list = res.data.list
						let nowTime = Math.round(new Date().getTime() / 1000).toString()
						_list.forEach(item => {
							this.$set(item, 'checked', false)
							// 判断优惠券是否有效(开始时间)
							this.$set(item, 'disabled', item.startTime > nowTime ? true : false)
							this.$set(item, 'cla', item.disabled ? 'cci-l bg-c' : 'cci-l') // 绑定相应的class样式
						})
						this.userCoupons = _list
					}
				})
			},
			// 点击使用/取消优惠券操作
			couponHandle(index) {
				// 更改使用/取消状态
				this.userCoupons[index].checked = !this.userCoupons[index].checked
				// 暂存当次选中使用的优惠券key
				this.optCoupon = this.userCoupons[index].couponCode
				let arr = []
				this.userCoupons.forEach(item => {
					if (item.checked) {
						arr.push(item.couponCode)
					}
				})
				if (this.userCoupons[index].checked) {
					// 使用
					this.params.couponCode = arr.join()
				} else {
					// 取消使用
					let paramsCodes = this.params.couponCode.split(',')
					let usedIndex = paramsCodes.indexOf(this.userCoupons[index].couponCode)
					if (usedIndex !== -1) {
						paramsCodes.splice(usedIndex, 1)
						this.params.couponCode = paramsCodes.join()
					}
				}
			},
			// 手输的优惠券码使用
			useInputCouponCode() {
				if (!this.inputCouponCode) {
					this.$u.toast('请输入优惠券码')
				} else {
					// 判断是否有使用的优惠券
					if (this.params.couponCode.length > 0) {
						this.params.couponCode += ',' + this.inputCouponCode
					} else {
						this.params.couponCode = this.inputCouponCode
					}
				}
			},
			// 不使用优惠券
			notUseCoupon() {
				this.inputCouponCode = '' // 清空手输的优惠券码
				this.userCoupons.forEach(item => {
					item.checked = false
				}) // 取消所有选中的使用状态
				this.params.couponCode = '' // 清空params优惠券码
			},
			// 移除/取消使用中的指定优惠券
			removeCouponCode(code, current) {
				let arr = this.params.couponCode.split(',')
				arr.splice(arr.indexOf(code), 1)
				current === 0 ? this.optCoupon = '' : this.inputCouponCode = ''
				this.params.couponCode = arr.join()
			},
			// 是否使用积分
			changePointHandle(e) {
				//this.switchA = e.detail.value
				if (this.userPointNums > 0) {
					this.isUsePoint = !this.isUsePoint;
					this.params.point = this.isUsePoint ? this.canUsePoint : 0;
				}
			},
			// 去支付
			toPay() {
				this.submitStatus = true;
				let receiptType = 1;
				if (this.OrderDistributionModelCurrent == 'isOpenMailing') {
					receiptType = 1;
				} else if (this.OrderDistributionModelCurrent == 'isOpenHomeDelivery') {
					receiptType = 2;
				} else if (this.OrderDistributionModelCurrent == 'isOpenSelfDelivery') {
					receiptType = 3;
				}
				this.receiptType = receiptType;
				let data = {
					cartIds: this.params.ids,
					memo: this.memo,
					couponCode: this.params.couponCode,
					point: this.params.point,
					receiptType: this.receiptType,
					objectId: this.objectId,
					teamId: this.teamId,
					orderType: this.orderType, //订单类型
					scene: this.$store.state.scene, //场景值（用于确定小程序是否来源直播和视频号）
					requireOrder: this.requireOrder,
					requiredFundType: this.requiredFundType,
					traceId: this.traceId,
				}

				let delivery = {}
				// 判断是快递配送还是门店自提
				if (this.receiptType == 1 || this.receiptType == 2) {
					if (!this.userShip.id || !this.params.areaId) {
						this.$u.toast('请选择收货地址');
						this.submitStatus = false;
						return false;
					}
					// 快递配送
					delivery = {
						ushipId: this.userShip.id,
						areaId: this.params.areaId
					}
				}
				if (this.receiptType == 3) {
					if (!this.store.id) {
						this.$u.toast('请选择自提门店');
						this.submitStatus = false;
						return false;
					}
					if (!this.storePick.name) {
						this.$u.toast('请输入提货人姓名');
						this.submitStatus = false;
						return false;
					}
					if (!this.storePick.mobile) {
						this.$u.toast('请输入提货人电话');
						this.submitStatus = false;
						return false;
					}
					// 门店自提
					delivery = {
						storeId: this.store.id,
						ladingName: this.storePick.name,
						ladingMobile: this.storePick.mobile
					}
				}

				// 发票信息
				data['taxType'] = this.invoice.type
				data['taxName'] = this.invoice.name
				data['taxCode'] = this.invoice.code
				// #ifdef MP-WEIXIN
				data['source'] = 3;
				// #endif
				// #ifdef MP-ALIPAY
				data['source'] = 4;
				// #endif
				// #ifdef APP-PLUS || APP-PLUS-NVUE
				data['source'] = 5;
				// #endif
				// #ifdef MP-TOUTIAO
				data['source'] = 6;
				// #endif
				data = Object.assign(data, delivery)
				this.$u.api.createOrder(data).then(res => {
					this.submitStatus = false;
					if (res.status) {
						// 创建订单成功 去支付
						// 判断是否为0元订单,如果是0元订单直接支付成功
						if (res.data.payStatus == '2') {
							this.$u.route({
								type: 'redirectTo',
								url: '/pages/payment/result/result?orderId=' + res.data.orderId
							});
						} else {
							this.$u.route({
								type: 'redirectTo',
								url: '/pages/payment/pay/pay?orderId=' + res.data.orderId + '&type=' + this
									.orderType
							});
						}
						//发起订阅
						// #ifdef MP-WEIXIN
						this.subscription();
						// #endif
					} else {
						this.$u.toast(res.msg);
					}
				});
			},
			//发起订阅
			subscription() {
				this.$u.api.getSubscriptionTmplIds(null).then(res => {
					if (res.status) {
						//console.log(res.data);
						if (res.data.length > 0) {
							wx.getSetting({
								withSubscriptions: true,
								success(resSetting) {
									//console.log('subscriptionsSetting', resSetting.subscriptionsSetting)
									if (wx.requestSubscribeMessage) {
										wx.requestSubscribeMessage({
											tmplIds: res.data, //购买，发货，收货通知
											success: function(ress) {
												//console.log(ress);
											},
											fail: function(ress) {
												//console.log(ress);
											}
										})
									} else {
										wx.showToast({
											title: '请更新您微信版本，来获取订阅消息功能',
											icon: 'none'
										});
									}

								}
							});
						}
					} else {
						this.$u.toast('消息订阅配置信息获取失败');
					}
				});
			},
			// 跳转发票页面
			goInvoice() {
				this.$u.route('/pages/placeOrder/invoice/invoice')
			},
			// 跳转我的收货地址列表
			showAddressList() {
				this.$u.route('/pages/member/address/list/list?type=order')
			},
			// tab点击切换
			onClickItem(index) {
				if (this.current !== index) {
					this.current = index;
				}
			},
			// 获取默认店铺
			getDefaultStore() {
				if (this.storeSwitch == 1) {
					this.$u.api.defaultStore().then(res => {
						if (res.status) {
							if (res.data && res.data.id) {
								let store = {
									id: res.data.id || 0,
									name: res.data.storeName || '',
									mobile: res.data.mobile || '',
									address: res.data.address || ''
								}
								this.store = store;
							} else {
								this.$u.toast('商家未配置默认自提店铺！');
							}
						}
					});
				}
			},
			memoChange(e) {
				//console.log(e);
				this.memo = e.detail.value
			},
		},
		computed: {
			...mapState({
				userInfo: state => state.userInfo,
			}),
			userInfo: {
				get() {
					return this.$store.state.userInfo
				},
				set(val) {
					this.$store.commit('userInfo', val)
				}
			},
			// 计算购物车商品数量
			productNums() {
				let nums = 0
				for (let i in this.cartData.list) {
					if (this.cartData.list[i].isSelect) {
						nums += this.cartData.list[i].nums;
					}
				}
				return nums
			},
			// 判断商户是否开启积分抵扣 1开启 2未开启
			isOpenPoint() {
				return this.$store.state.config.pointSwitch
			},
			// 判断是否开启了我的优惠券 1开启 2未开启
			isShowCoupon() {
				return this.$store.state.config.showCoupon == 1;
			},
			// 获取使用的优惠券名称
			usedCouponsCompute() {
				var userCouponsCount = this.userCoupons.length;

				let name = userCouponsCount + '张可用'
				if (Object.keys(this.usedCoupons).length) {
					let coupons = []
					for (let i in this.usedCoupons) {
						coupons.push(this.usedCoupons[i])
					}
					name = coupons.join()
				}
				return name
			},
			// 判断是否开启发票功能
			invoiceSwitch() {
				//console.log("发票功能:" + this.$store.state.config.invoiceSwitch);
				return this.$store.state.config.invoiceSwitch || 2;
			},
			// 判断店铺开关
			storeSwitch() {
				//var status = this.$store.state.config.storeSwitch || 2;
				//if (status == 2) {
				//    this.typeItem = ['快递物流', '同城配送']
				//}
				//console.log(status);
				return this.$store.state.config.storeSwitch || 2;
			},
			// 根据接口返回数据判断是否使用优惠券
			couponIsUsed() {
				//console.log(this.cartData.coupon);
				return this.cartData.coupon && this.cartData.coupon.length > 0;
			},
			pointSwitch() {
				return this.$store.state.config.pointSwitch
			},
			pointShowExchangePrice() {
				return this.$store.state.config.pointShowExchangePrice
			},
			pointDiscountedProportion() {
				return this.$store.state.config.pointDiscountedProportion
			},
			pointExchangeModel() {
				return this.$store.state.config.pointExchangeModel
			},
			pointShowName() {
				return this.$store.state.config.pointShowName
			},
			pointGetModel() {
				return this.$store.state.config.pointGetModel
			},
			pointShowPoint() {
				return this.$store.state.config.pointShowPoint
			},
		},
		watch: {
			// 监听数据状态(切换收货地址, 是否使用优惠券, 是否使用积分) 重新请求订单数据
			params: {
				handler() {
					this.getCartList();
				},
				deep: true
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'index.scss';
</style>